package com.hou.msmservice.controler;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hou.commonutils.R;
import com.hou.commonutils.RandomUtil;
import com.hou.msmservice.service.MsmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author shkstart
 * @create 2021-11-18-21:35
 */
// 由于各大平台短信验证服务不对个人用户开放
// 这里使用将数据写死的方法来代替短信登录
@RequestMapping("/edumsm/msm")
@RestController
@CrossOrigin
public class MsmController {

    @Autowired
    private MsmService msmService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    // 伪造发短信
    @GetMapping("send/{phone}")
    public R send (@PathVariable String phone) throws Exception {

        String redisCode = redisTemplate.opsForValue().get(phone);

        if (!StringUtils.isEmpty(redisCode)){
            return R.ok();
        }

        // 随机生成六位验证码
        String sixBitRandom = RandomUtil.getSixBitRandom();


        boolean isSend = msmService.send(sixBitRandom,phone);

        if (!isSend) {
            throw new Exception("发送短信失败！");
        }

        return R.ok();
    }


}
